package com.mybatis.util;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.Map;

import org.apache.ibatis.type.JdbcType;

public class MybatisHelper {
    public static String getJdbcType(Field field, Object args) {
        if (args == null || !(args instanceof Map)) {
            return "";
        }
        Map<String, Object> map = (Map) args;
        if (!map.containsKey(field.getName()) || map.get(field.getName()) != null) {
            return "";
        }
        if (field.getType() == String.class) {
            return String.valueOf(", jdbcType=") + JdbcType.VARCHAR;
        }
        if (field.getType() == Integer.class || field.getType() == Integer.TYPE) {
            return String.valueOf(", jdbcType=") + JdbcType.INTEGER;
        }
        if (field.getType() == Long.class || field.getType() == Long.TYPE) {
            return String.valueOf(", jdbcType=") + JdbcType.BIGINT;
        }
        if (field.getType() == Float.class || field.getType() == Float.TYPE) {
            return String.valueOf(", jdbcType=") + JdbcType.FLOAT;
        }
        if (field.getType() == Double.class || field.getType() == Double.TYPE) {
            return String.valueOf(", jdbcType=") + JdbcType.DOUBLE;
        }
        if (field.getType() == Date.class) {
            return String.valueOf(", jdbcType=") + JdbcType.DATE;
        }
        return ", jdbcType=";
    }

    public static String getExecuSQL(Class<?> clazz, String mapperDBsql, Object param) throws Exception {
        if (mapperDBsql.equals("MapperGD.insert.entity")) {
            return SQLUtil.insertEntityBatch(clazz, false, null, null);
        }
        if (mapperDBsql.equals("MapperGD.find.entity")) {
            return SQLUtil.findEntity(clazz, param);
        }
        if (mapperDBsql.equals("MapperGD.find.entitys")) {
            return SQLUtil.findEntity(clazz, param);
        }
        if (mapperDBsql.equals("MapperGD.find.entityById")) {
            return SQLUtil.findEntity(clazz, SQLUtil.getTableKeyName(clazz));
        }
        if (mapperDBsql.equals("MapperGD.update.entity")) {
            return SQLUtil.updateEntityById(clazz, param);
        }
        if (mapperDBsql.equals("MapperGD.update.map")) {
            return SQLUtil.updateEntityById(clazz, param);
        }
        if (mapperDBsql.equals("MapperGD.delete.id")) {
            return SQLUtil.deleteEntity(clazz, null);
        }
        if (mapperDBsql.equals("MapperGD.delete.condition")) {
            return SQLUtil.deleteEntity(clazz, param);
        }
        return null;
    }

	public static Class getEntityClazz(String resourceType) {
		Class cc = null;
		Class rr = null;

		try {
			String e = resourceType.replaceAll("/", ".");
			e = e.substring(0, e.indexOf(".java"));
			cc = MybatisHelper.class.getClassLoader().loadClass(e);
			Type[] ccT = cc.getGenericInterfaces();
			if (ccT != null && ccT.length > 0) {
				Type[] var8 = ccT;
				int var7 = ccT.length;

				for (int var6 = 0; var6 < var7; ++var6) {
					Type type = var8[var6];
					if (type instanceof ParameterizedType) {
						ParameterizedType aType = (ParameterizedType) type;
						Type[] parameterArgTypes = aType
								.getActualTypeArguments();
						rr = (Class) parameterArgTypes[0];
					}
				}
			}
		} catch (Exception var11) {
			var11.printStackTrace();
		}

		return rr;
	}
}
